<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <liujiqi@etrol.com>
// +----------------------------------------------------------------------

namespace app\rehearse\controller;

use app\common\controller\Admin;
use app\rehearse\controller\api\Record as RecordApi;
use app\rehearse\controller\api\Recorddetail as RecorddetailApi;
use app\admin\controller\ajax\Library as Ajax;


/**
 * 演练执行
 * @package app\plan
 */
class Evaluate  extends Admin

{

    protected $api;

    public function _initialize()
    {
        parent::_initialize();
        $this->detail = new RecorddetailApi();
        $this->ajax= new Ajax();
        $this->api = new RecordApi();

    }
    /**
     * 首页
     *
     */
    public function index() {
        if(IS_AJAX){
            $recordList = $this->api->recordList();
            return $recordList;
        }

        return $this->fetch();
    }

    /**
     * 演练执行搜索
     */
    public function search($start=null,$end=null,$keyword=null) {
        if(IS_AJAX){
            $recordList = $this->api->search(input('start'),input('end'),input('keyword'));
            return $recordList;
        }
    }
    /**
     * 演练执行评估
     *
     */
    public function evaluateRecord($recordId=null) {
        if(IS_POST){
            $evaluate = input('post.evaluate');
            $id = input('post.id');
            $score = input('post.score');
            $res = model('rehearse/Record')->where(array('id'=>$recordId))->update(['evaluate'=>$evaluate,'score'=>$score]);
            if($res !== false){
                return json(['code'=>1,'msg'=>'评估成功']);
            }else{
                return json(['code'=>0,'msg'=>'评估失败,请检查']);

            }
        }
        $recordlist=$this->api->recordList(input('recordId'))->getData();
        $this->assign('info',$recordlist['data'][0]);
        $this->assign('recordId',$recordId);

        return $this->fetch();
    }


    /**
     * 演练执行明细评估
     *
     */
    public function evaluateRecorddetail($recordId=null,$recorddetailId=null) {
        if(IS_POST){
            $evaluate = input('post.evaluate');

            $id=input('post.id');

            $res=model('rehearse/Recorddetail')->where(array('id'=>$id))->update(['evaluate'=>$evaluate]);
            if($res){
                 return json(['code'=>1,'msg'=>'评估成功']);
              }else{
                 return json(['code'=>0,'msg'=>'评估失败,请检查']);

             }
            }
        $recorddetaillist=$this->detail->recorddetailList(input('recordId'),input('recorddetailId'))->getData();
        $this->assign('info',$recorddetaillist['data'][0]);


        $this->assign('recordId',$recordId);
        return $this->fetch();
    }

}



